object rationals {;import org.scalaide.worksheet.runtime.library.WorksheetSupport._; def main(args: Array[String])=$execute{;$skip(47); 
  val x = new Rational(4, 6);System.out.println("""x  : Rational = """ + $show(x ));$skip(10); val res$0 = 
  x.denom;System.out.println("""res0: Int = """ + $show(res$0));$skip(10); val res$1 = 
  x.numer;System.out.println("""res1: Int = """ + $show(res$1));$skip(31); 

  val y = new Rational(2, 3);System.out.println("""y  : Rational = """ + $show(y ));$skip(19); 
  val z = x.add(y);System.out.println("""z  : Rational = """ + $show(z ));$skip(10); val res$2 = 

  z.neg;System.out.println("""res2: Rational = """ + $show(res$2));$skip(11); val res$3 = 
  z.sub(x);System.out.println("""res3: Rational = """ + $show(res$3))}
}

class Rational(x: Int, y: Int) {
  require(y != 0, "ZERO division!")
  def this(x: Int) = this(x, 1)

  private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
  private val g = gcd(x, y)

  def numer = x
  def denom = y

  def less(that: Rational) = numer * that.denom < that.numer * denom

  def max(that: Rational) = if (this.less(that)) that else this

  def add(that: Rational) =
    new Rational(numer * that.denom + that.numer * denom, denom * that.denom)

  override def toString = x / g + "/" + y / g

  def neg() = new Rational(-numer, denom)

  def sub(that: Rational) = add(that.neg)

}
